package demo.string;

import com.sun.deploy.util.StringUtils;

public class CountStr {
    public static void main(String[] args) {
        System.out.println(getCountString("aaabbadddffc"));
        //a_3_b
        //a_3_b_2_a
        //a_3_b_2_a_1_d
        //a_3_b_2_a_1_d_3_f
        //a_3_b_2_a_1_d_3_f_2_c
    }

    public static String getCountString(String str) {
        if (str == null || str.equals("")) {
            return "";
        }

        char[] chars = str.toCharArray();
        String res = String.valueOf(chars[0]);
        int num = 1;
        for (int i = 1; i < chars.length; i++) {
            if (chars[i] != chars[i - 1]) {
                res = concat(res, String.valueOf(num), String.valueOf(chars[i]));
                num = 1;
            } else {
                num++;
            }
        }

        return concat(res, String.valueOf(num), "");
    }

    public static String concat(String s1, String s2, String s3) {
        return s1 + "_" + s2 + (s3.equals("") ? s3 : "_" + s3);
    }
}
